package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seawaterbt.ssm.entity.ComplainInfo;
import com.seawaterbt.ssm.entity.ResultInfo;
import com.seawaterbt.ssm.entity.ZComplainInfo;
import com.seawaterbt.ssm.entity.ZResultInfo;
import com.seawaterbt.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class ZhoneResultInfo {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    ZResultInfoService zResultInfoService;
    @Autowired
    IResultInfoService resultInfoService;
    @Autowired
    IComplainInfoService complainInfoService;
    @Autowired
    ZComplainInfoService zComplainInfoService;

    public void pull() {
        QueryWrapper<ZResultInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wdtIsSync", 0).eq("isOther", 1);
        List<ZResultInfo> resultInfoList = zResultInfoService.list(queryWrapper);
        if (resultInfoList != null) {
            for (ZResultInfo zResultInfo : resultInfoList) {
                ResultInfo resultInfo = new ResultInfo();
                QueryWrapper<ComplainInfo> complainInfoQueryWrapper = new QueryWrapper<>();
                complainInfoQueryWrapper.eq("code_city", zResultInfo.getItemId());
                ComplainInfo complainInfo = complainInfoService.getOne(complainInfoQueryWrapper);
                if (complainInfo != null) {
                    resultInfo.setComplainId(complainInfo.getId());
                    resultInfo.setProgressContent(zResultInfo.getContent());
                    resultInfo.setResultCodeCity(zResultInfo.getCityCode());
                    resultInfo.setState(zResultInfo.getState());
                    resultInfoService.save(resultInfo);
                    if (zResultInfo.getStatus() == 2) {//完结更新事项状态
                        QueryWrapper<ZComplainInfo> zComplainInfoQueryWrapper = new QueryWrapper<>();
                        zComplainInfoQueryWrapper.eq("TransferItemCityID", zResultInfo.getItemId());
                        ZComplainInfo zComplainInfo = zComplainInfoService.getOne(zComplainInfoQueryWrapper);
                        complainInfo.setCloseTime(zComplainInfo.getRepairTime());
                        complainInfo.setStatus(2);
                        complainInfoService.saveOrUpdate(complainInfo);
                    }
                    zResultInfo.setWdtIsSync(1);
                    zResultInfoService.saveOrUpdate(zResultInfo);
                }
            }
        }

    }


}
